<?php

define('METHOD_FRIENDMANAGER', 'fm');
define('METHOD_ORGANIZERMANAGER', 'om');
define('METHOD_GROUPMANAGER', 'gm');
define('METHOD_ACCOUNTMANAGER', 'am');
define('METHOD_DESTINYMANAGERORGANIZATION', 'dmo');
define('METHOD_USERMANAGER', 'um');
define('METHOD_DIARY', 'em');


//Comprobar parámetros de login.
if(!empty($_GET['email']) && is_string($_GET['email'])
	&& !empty($_GET['pass']) && is_string($_GET['pass'])
		&& !empty($_GET['method']) && is_string($_GET['method'])){
		
	$email = $_GET['email'];
	$password = $_GET['pass'];
	$action = explode('.', $_GET['method']);
	$method = $action[0];
	$action = $action[1];
	
	if(strcmp($method, METHOD_ACCOUNTMANAGER) == 0){
		require($path . '/actions/accountmanager.php');
	} else {
		$user = new User();
		if(is_email($email)){
			if($user->login($email, $password)){ //Good login
				switch(true){
					case (strcmp($method, METHOD_FRIENDMANAGER) == 0):
						require($path . '/actions/friendmanager.php');
						break;
					case (strcmp($method, METHOD_ORGANIZERMANAGER) == 0):
						require($path . '/actions/organizatormanager.php');
						break;
					case (strcmp($method, METHOD_GROUPMANAGER) == 0):
						require($path . '/actions/groupmanager.php');
						break;
					case (strcmp($method, METHOD_DESTINYMANAGERORGANIZATION) == 0):
						require($path . '/actions/destinymanagerorganization.php');
						break;
					case (strcmp($method, METHOD_USERMANAGER) == 0):
						require($path . '/actions/usermanager.php');
						break;
					case (strcmp($method, METHOD_DIARY) == 0):
						require($path . '/actions/placemanagerdestiny.php');
						break;
					default:
						$result = array('status' => ERR_GENERAL);
				}
			} else { //bad login
				$result = array('status' => ERR_LOGIN);
			}
		} else {
			$result = array('status' => ERR_INVALID_EMAIL);			
		}
	}
} else {
	$result = array('status' => ERR_GENERAL);
}

if(isset($result)) echo json_encode($result, JSON_NUMERIC_CHECK);
if(isset($_GET['debug'])){
	echo '<pre>';
	print_r($result);
	echo '</pre>';
}

?>